//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@infratel.com), 2002-2004
//
// Change comment attribute for object.
//

# include "StdAfx.h"
# include "UndoRedo.h"
# include "SetComment.h"
# include "Label.h"
# include "Globals.h"

class CSetCommentAction : public CEditAction {
  public:
	CSetCommentAction (rgn_t & _rgn, const string_t & _strNewComment) :
		m_rgn (_rgn),
		m_strNewComment (_strNewComment),
		m_strPrevComment (_rgn.strComment),
		m_bWasModified ((_rgn.dwFlags & rgn_t::fModified) != 0)
	{}

	virtual void GetName (string_t & _str) const;
	virtual cu_t CanUndo () const {return cuYes;}
	virtual bool Do ();
	virtual void Undo ();
	virtual void Redo ();

  private:
	rgn_t &  m_rgn;
	string_t const m_strNewComment;
	string_t const m_strPrevComment;
	bool     const m_bWasModified;
};

void CSetCommentAction::GetName (string_t & _str) const {
	CCritSec::CLock lock (g_cs);

	_str.append ("change comment [");
	GetHintText (m_rgn, _str);
	_str.append ("]");
}

bool CSetCommentAction::Do () {
	CCritSec::CLock lock (g_cs);

	// Update comment' text.
	m_rgn.strComment = m_strNewComment;

	m_rgn.dwFlags |= rgn_t::fModified;

	return true;
}

void CSetCommentAction::Undo () {
	CCritSec::CLock lock (g_cs);

	// Revert comment' text
	m_rgn.strComment = m_strPrevComment;

	if (! m_bWasModified)
		m_rgn.dwFlags &= ~rgn_t::fModified;
}

void CSetCommentAction::Redo () {
	CSetCommentAction::Do ();
}

/////////////////////////////////////////////////////////////////

void SetComment (rgn_t & _rgn, const string_t & _strComment) {
	PerformEditAction (new CSetCommentAction (_rgn, _strComment));
}
